package com.dhl.fin.api.domain;

import com.dhl.fin.api.common.annotation.Excel;
import com.dhl.fin.api.common.annotation.ExcelTitle;
import com.dhl.fin.api.common.domain.BasicDomain;
import com.dhl.fin.api.common.util.DateUtil;
import com.dhl.fin.api.dto.LastMonthInterestStatementDTO;
import com.dhl.fin.api.enums.BusinessTypeEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.*;

/**
 * @author CuiJianbo
 * @since 2025/3/15
 */

/**
 * 银行流水
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Excel
@Table(name = "t_bank_statement")
public class BankStatement extends BasicDomain {

    @ExcelTitle(name = "业务名字", width = 15, sort = 1)
    @Column(columnDefinition = "varchar(10)")
    private String typeName;

    @ExcelTitle(name = "交易金额（元）", width = 15, sort = 2)
    @Column(columnDefinition = "decimal(10,2)")
    private Double moneyAmount;

    @ExcelTitle(name = "当前余额（元）", width = 15, sort = 3)
    @Column(columnDefinition = "decimal(10,2)")
    private Double currentBalance;

    private Integer year;

    private Integer month;

    @ExcelTitle(name = "标签", width = 25, sort = 6)
    @Column(columnDefinition = "varchar(50)")
    private String tag;

    @ExcelTitle(name = "备注", width = 25, sort = 7)
    @Column(columnDefinition = "varchar(200)")
    private String remark;

    @ExcelTitle(name = "办理人", width = 15, sort = 4)
    @Column(columnDefinition = "varchar(10)")
    private String operator;

    @Transient
    @ExcelTitle(name = "办理时间", dateFormat = "yyyy-MM-dd HH:mm:ss", width = 20, sort = 5)
    private String createTimeStr;

    @ManyToOne
    @JoinColumn(name = "bank_account_id")
    private BankAccount bankAccount;


    public static BankStatement convertToLastMonthInterestStatementDTO(LastMonthInterestStatementDTO lastMonthInterestStatementDTO) {
        BankStatement bankStatement = new BankStatement();
        BankAccount bankAccount = new BankAccount();
        int month = DateUtil.getCurrentMonth();
        int year = DateUtil.getCurrentYear();

        bankAccount.setId(lastMonthInterestStatementDTO.getBankAccountId());
        bankStatement.setBankAccount(bankAccount);
        bankStatement.setRemark(lastMonthInterestStatementDTO.getMonth() + "月活期利息结算");
        bankStatement.setMoneyAmount(lastMonthInterestStatementDTO.getTotalInterest());
        bankStatement.setOperator("system");
        bankStatement.setTypeName(BusinessTypeEnum.TIME_INTEREST.getName());
        bankStatement.setYear(year);
        bankStatement.setMonth(month);
        bankStatement.setCurrentBalance(lastMonthInterestStatementDTO.getNewCurrentBalance());

        return bankStatement;
    }
}
